/*
    Copyright (C) 2010-2011  Anton Dedkov (Antonded). antonded@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "fsnode.h"

FSNode::FSNode (string NodeName /* = ""*/, FSNode* parent /* = NULL*/)
{
	NodeName_ = NodeName;
	parent_   = parent;
}

FSNode::~FSNode ()
{
	for (;dirs_.empty() == false;)
	{
		delete dirs_.back ();
		dirs_.pop_back ();
	}
	
	for (;files_.empty() == false;)
	{
		delete files_.back ();
		files_.pop_back ();
	}
}

void FSNode::AddDir (string dirname)
{
	if  (dirname == "") return;
	else
	{
		string folder   = "";
		
		if (dirname[0] == '/') dirname.erase (dirname.begin());
		
		
		for (; dirname.size () > 0 && dirname[0] != '/';)
		{
			folder.push_back (dirname[0]);
			dirname.erase (dirname.begin());
		}
		
		if (dirname[0] == '/') dirname.erase (dirname.begin());
		
		bool unique = true;
		for (list<FSNode*>::iterator i = dirs_.begin (); i != dirs_.end(); i++)
		{
			if ((*i)->NodeName_ == folder)
			{
				unique = false;
				break;
			}
		}
		for (list<FSFile*>::iterator i = files_.begin (); i != files_.end(); i++)
		{
			if ((*i)->filename_ == folder)
			{
				unique = false;
				break;
			}
		}
		
		if (unique && folder != "") dirs_.push_back (new FSNode (folder, this));
		
		dirs_.back()->AddDir (dirname);
	}
}

void FSNode::AddFile (string filename, string dirname)
{
	if (filename == "") return;
  
	if (dirname == "")
	{
		files_.push_back(new FSFile (filename, this));
	
		return;
	}
	 if (dirname[0] == '/')
	{
		dirname.erase(dirname.begin());
		if (dirname == "") AddFile (filename, dirname);
		
		string dir = "";
		
		for (; dirname.size () > 0 && dirname[0] != '/';)
		{
			dir.push_back (dirname[0]);
			dirname.erase (dirname.begin());
		}
		if (dirname[0] == '/') dirname.erase (dirname.begin());
		
		if (dir != "")
		{
			for (list<FSNode*>::iterator i = dirs_.begin (); i != dirs_.end(); i++)
			{
				if ((*i)->NodeName_ == dir)
				{
				    (*i)->AddFile (filename, dirname);
					return;
				}
			}
			dirs_.push_back (new FSNode (dir, this));
		
			dirs_.back()->AddFile (filename, dirname);
		}
	}
}

void FSNode::DeleteDir (FSNode* dir)
{
	if (dir)
	{
		for (;dir->dirs_.empty() == false;)
		{
			delete dir->dirs_.back ();
			dir->dirs_.pop_back ();
		}
		
		for (;dir->files_.empty() == false;)
		{
			delete dir->files_.back ();
			dir->files_.pop_back ();
		}
		
		dir->parent_->dirs_.remove (dir);
		delete dir;
	}
}

void FSNode::DeleteDir (string dir)
{
	
}

void FSNode::DeleteFile (FSFile* file)
{
    file->parent_->files_.remove(file);
	delete file;
}

void FSNode::DeleteFile (string filename, string dirname)
{
	
}

string FSNode::MakeFsString ()
{
	string str = "";
	
	str += "n(\"";
	str += NodeName_;
	str += "\"";
	
	for (list<FSNode*>::iterator i = dirs_.begin (); i != dirs_.end(); i++)
	{
		str += ",";
		str += (*i)->MakeFsString ();
	}
	
	for (list<FSFile*>::iterator i = files_.begin (); i != files_.end(); i++)
	{
		str += ",";
		str += (*i)->MakeFsString ();
	}
			
	str += ")";
	
	
	return str;
}

void FSNode::LoadFromString(string str, int position)
{
	
}

